﻿@{
    ViewBag.Title = "数据质量列表";
    Layout = "~/Views/Shared/_Main.cshtml";
}
@section Head{
    <style>
        .viewDQ
        {
            display: none;
        }
    </style>
    <script type="text/javascript">
        var UrlArray = $.getUrlArray();
        var nmID = UrlArray["nmID"];
        var ndbName = UrlArray["ndbName"];
        var opts = {
            height: 450,
            rows: 10,
            idField: "GUID",
            url: "/DQConfig/GetList",
            idField: "GUID",
            columns: [
            { field: 'DQ_Name', title: '数据质量名称', width: 30, sortable: true },
            { field: 'DQ_Description', title: '数据质量描述', width: 40, sortable: true },
            { field: 'CreateTime', title: '创建时间', width: 10, align: 'center', sortable: true },
            { field: 'SortNum', title: '排序', width: 7, align: 'center', formatter: function (value, rowData, rowIndex) {
                if (UrlArray["viewDQ"] == "1") {
                    return value;
                } else
                    return "<select onchange='DoSort(this.value," + rowIndex + ")' class='sort sel" + value + "'></select>";
            }
            },
            { field: 'GUID', title: '操作', width: 13, align: 'center', formatter: function (value, rowData, rowIndex) {
                var a = "<select onchange='DoAction(this.options.selectedIndex,\"" + value + "\")'><option value=''>-请选择-</option>";
                var add = true; ;
                for (var j = 0; j < retData.length; j++) {
                    if (retData[j].GUID == value) {
                        add = false;
                    }
                }
                if (add) {
                    retData.push(rowData);
                }
                var viewdq = "";
                if (UrlArray["viewDQ"] == "1") {
                    return a + "<option>查看数据</option></select>";
                } else
                    return a + "<option>修改</option><option>删除</option></select>";
            }
            }
            ],
            OnLoaded: function () {
                InitSort();
            }
        };
        $(function () {
            $(".link a").each(function () {
                $(this).attr("href", $(this).attr("href") + "?modelID=" + UrlArray["modelID"] + "&dbName=" + encodeURI(UrlArray["dbName"])
                + "&nmID=" + UrlArray["nmID"] + "&ndbName=" + encodeURI(UrlArray["ndbName"]));
            })
            if (UrlArray["viewDQ"] == "1") {
                $(".viewDQ").show();
                $(".link").hide();
                nmID = "";
                ndbName = "";
            }
            InitList();
        });
        var retData = new Array();
        function InitList() {
            opts.para = { modelID: UrlArray["modelID"], dbName: encodeURI(UrlArray["dbName"]), nmID: encodeURI(nmID), ndbName: encodeURI(ndbName) };
            $("#DataGrid").dg(opts);
        }
        function AddItem(v) {
            window.location.href = "/DQConfig/Edit?id=" + v + "&modelID=" + UrlArray["modelID"] + "&dbName=" + encodeURI(UrlArray["dbName"]) + "&nmID=" + UrlArray["nmID"] + "&ndbName=" + encodeURI(UrlArray["ndbName"]);
        }
        function save(v) {
            var modelID = UrlArray["modelID"];
            var dbName = UrlArray["dbName"];

            $.post("/DQConfig/UpStep", { data: $.toJSON(retData), modelID: modelID, nmID: UrlArray["nmID"], ndbName: encodeURI(UrlArray["ndbName"]), dbName: encodeURI(dbName) }, function (result) {
                if (result == "1") {
                    if (v == "next")
                        goto();
                    else
                        alert("保存成功！");
                } else
                    alert(result);
            });
        }
        function goto() {
            window.location.href = "/ModelConfig/EditColumn?modelID=" + UrlArray["modelID"] + "&dbName=" + encodeURI(UrlArray["dbName"]) + "&nmID=" + UrlArray["nmID"] + "&ndbName=" + encodeURI(UrlArray["ndbName"]);
        }
        function back() {
            window.location.href = "/ODSConfig/ODSConfigAdd?modelID=" + UrlArray["modelID"] + "&dbName=" + encodeURI(UrlArray["dbName"]) + "&nmID=" + UrlArray["nmID"] + "&ndbName=" + encodeURI(UrlArray["ndbName"]);
        }

        function DoAction(i, v) {
            if (i == 1) {
                if (UrlArray["viewDQ"] == "1")
                    showModalMany('Info', '/GenericSearch/DQItem?modelID=' + UrlArray["modelID"] + '&DQID=' + v + '&dbName=' + encodeURI(UrlArray["dbName"]), '800', '400');
                else
                    AddItem(v);
            }
            if (i == 2) {
                DeleteItem(v);
            }
            InitList();
        }
        function DeleteItem(v) {
            if (confirm("确定要删除吗?")) {
                $.post("/DQConfig/Delete", { id: v, dbName: encodeURI(UrlArray["dbName"]) }, function (result) {
                    if (result == "1") {
                        var ri = 0;
                        for (var i = 0; i < retData.length; i++) {
                            if (retData[i].GUID == v)
                                ri = i;
                        }
                        retData.removeAt(ri);
                        alert("删除成功！");
                    } else
                        alert(result);
                    InitList();
                });
            }
        }
        function InitSort(v) {
            var sel = "";
            for (var i = 1; i <= retData.length; i++) {
                sel += "<option value='" + i + "'>" + i + "</option>";
            }
            $(".sort").append(sel);
            $(".sort").each(function () {
                var v = $(this).attr("class");
                v = v.replace("sort sel", "");
                $(".sel" + v).val(v);
            })
        }
        function DoSort(v, oi) {
            retData[oi].SortNum = v;
            //下去v>i+1
            if (v > oi + 1) {
                for (var i = 0; i < retData.length; i++) {
                    if (i > oi && i <= v - 1) {
                        retData[i].SortNum--;
                    }
                }
            }
            else {
                for (var i = 0; i < retData.length; i++) {
                    if (i >= v - 1 && i < oi) {
                        retData[i].SortNum++;
                    }
                }
            }
            retData.sort(function (a, b) { return a.SortNum - b.SortNum; });
            var data = { "total": 1, "rows": retData };
            opts["data"] = data;
            $("#DataGrid").dg(opts);
        }
    
    </script>
}
<div style="margin: 5px;">
    <table class="listTitle link">
        <tr>
            <td width="15">
                <img src="/images/filesave.png" />
            </td>
            <td width="500">
                <a href="/ModelConfig/Index">模型管理</a> --> <a href="/ModelConfig/Edit">新增模型配置</a>
                --> <a href="/ODSConfig/ODSConfigAdd">选择源系统表</a> --> 数据质量配置列表
            </td>
            <td align="right">
                <input type="button" class="button" value="新增条件" onclick="AddItem('')" />
            </td>
        </tr>
    </table>
    <table class="listTitle viewDQ">
        <tr>
            <td width="15">
                <img src="/images/search.png" />
            </td>
            <td width="500">
                数据质量配置列表
            </td>
            <td align="right">
            </td>
        </tr>
    </table>
    <table id="DataGrid" class="pagTable" cellpadding="0" cellspacing="0">
    </table>
    <div id="msg">
    </div>
</div>
<span class="link">
    <input type="button" class="button" value="上一步" onclick="back()" />
    <input type="button" class="button" value="保存" onclick="save()" />
    <input type="button" class="button" value="下一步" onclick="save('next')" />
</span>